package com.zego.wawaji.ui.dialog.invite;

import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.bean.Common;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.base.utils.InputUtils;
import com.zego.wawaji.ui.dialog.popup.BottomPopup;

/**
 * 输入邀请码弹框
 * Created by Su on 2017/12/23.
 */
public class InviteCodeDialog extends BottomPopup implements View.OnClickListener, TextWatcher {
    private String code;    // 获取剪切板邀请码
    private ImageView btnClear;
    private EditText editCode;

    public InviteCodeDialog(FragmentActivity activity) {
        super(activity);
        code = InputUtils.getTextFromClip();
    }

    @Override
    protected View makeContentView() {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.invite_code_dialog, null);

        btnClear = (ImageView) inflate.findViewById(R.id.btn_clear);
        btnClear.setOnClickListener(this);
        TextView tv_award_num = (TextView) inflate.findViewById(R.id.tv_award_num);
        Common common = ModuleMgr.getWawaMgr().getCommon();
        String awardNum = common.getInviteWwCoinZsYqNew();
        if (!TextUtils.isEmpty(awardNum)) {
            tv_award_num.setText("可获得娃娃币" + awardNum + "个");
        }

        editCode = (EditText) inflate.findViewById(R.id.edt_code);
        editCode.addTextChangedListener(this);
        if (!TextUtils.isEmpty(code)) {
            try {
                long codeNum = Long.valueOf(code);
                String strCode = String.valueOf(codeNum);
                editCode.setText(strCode);
                editCode.setSelection(strCode.length());
                btnClear.setVisibility(View.VISIBLE);
            } catch (Exception e) {
            }
        }

        inflate.findViewById(R.id.btn_submit).setOnClickListener(v -> {
            code = editCode.getText().toString();
            if (TextUtils.isEmpty(code)) {
                PToast.showShort("请输入邀请码");
                return;
            }
            ModuleMgr.getWawaMgr().reqYQCode(code);
            dismiss();
        });

        inflate.findViewById(R.id.btn_close).setOnClickListener(v -> dismiss());
        return inflate;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_clear:
                editCode.setText("");
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int num = s.toString().length();
        btnClear.setVisibility(num > 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}
